﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.Business.Implementation;
using BCMS.Settings;
using BCMS.FrontEnd.DataContract;

namespace BCMS.Forms.Functions
{
    public partial class frmCouponLocator : Form
    {
        CouponUI target = null;
        CouponUI sample = null;
        public frmMain _parentForm { set; get; }

        public frmCouponLocator()
        {
            InitializeComponent();
        }

        public frmCouponLocator(frmMain main)
        {
            this.Font = SystemFonts.MessageBoxFont;
            InitializeComponent();
            _parentForm = main;
        }

        private void txtTargetNum_Leave(object sender, EventArgs e)
        {
            try
            {
                getCouponInfo(txtTargetCode.Text + "-" + txtTargetNum.Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error getting coupon. Error: " + ex.Message);
            }
        }

        private void getCouponInfo(string serial)
        {
            try
            {
                DALclient client = new DALclient();
                target = client.getCouponBySerial(serial);

                if (target != null)
                {
                    txtTargetParValue.Value = Convert.ToInt64(target.ParValue);
                    txtTargetLastUpdated.Text = target.LastUpdated.ToString("dd/MM/yyyy,hh:mm:ss");

                    if (!target.Status.Equals(Params.CLAIMED))
                    {
                        target = null;
                        GlobalVars.ShowWarning("[CHƯA THU HỒI] phiếu: [" +
                                                txtTargetCode.Text + "-" + txtTargetNum.Text + "]");
                        ResetUI();
                        txtTargetCode.Focus();
                    }
                }
                else
                {
                    GlobalVars.ShowWarning("Không tìm thấy coupon: [" +
                                            txtTargetCode.Text + "-" + txtTargetNum.Text + "]");
                    ResetUI();
                    txtTargetCode.Focus();
                }
            }
            catch (Exception ex)
            {
                GlobalVars.ShowError("Lỗi thu hồi coupons: " + txtTargetCode.Text + "-" + txtTargetNum.Text +
                                "\nError: " + ex.Message);
            }
        }

        private void ResetUI()
        {
            txtTargetNum.Text = string.Empty;
            txtTargetParValue.Text = string.Empty;
            txtTargetLastUpdated.Text = string.Empty;
            txtTargetNum.Text = string.Empty;

            txtSampleCode.Text = string.Empty;
            txtSampleNum.Text = string.Empty;
            txtSampleParValue.Text = string.Empty;
            txtSampleLastUpdated.Text = string.Empty;
            txtSampleNum.Text = string.Empty;

            lblDistant.Text = string.Empty;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSampleNum_Leave(object sender, EventArgs e)
        {
            try
            {
                getSampleCouponInfo(txtSampleCode.Text + "-" + txtSampleNum.Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error getting coupon. Error: " + ex.Message);
            }
        }

        private void getSampleCouponInfo(string serial)
        {
            try
            {
                DALclient client = new DALclient();
                sample = client.getCouponBySerial(serial);

                if (sample != null)
                {
                    txtSampleParValue.Value = Convert.ToInt64(sample.ParValue);
                    txtSampleLastUpdated.Text = sample.LastUpdated.ToString("dd/MM/yyyy,hh:mm:ss");

                    if (!sample.Status.Equals(Params.CLAIMED))
                    {
                        sample = null;
                        GlobalVars.ShowWarning("[CHƯA THU HỒI] phiếu: [" +
                                                txtSampleCode.Text + "-" + txtSampleNum.Text + "]");
                        ResetSampleUI();
                        txtSampleCode.Focus();
                    }
                }
                else
                {
                    GlobalVars.ShowWarning("Không tìm thấy coupon: [" +
                                            txtSampleCode.Text + "-" + txtSampleNum.Text + "]");
                    ResetSampleUI();
                    txtSampleCode.Focus();
                }
            }
            catch (Exception ex)
            {
                GlobalVars.ShowError("Lỗi thu hồi coupons: " + txtSampleCode.Text + "-" + txtSampleNum.Text +
                                "\nError: " + ex.Message);
            }
        }

        private void ResetSampleUI()
        {
            txtSampleNum.Text = string.Empty;
            txtSampleParValue.Text = string.Empty;
            txtSampleLastUpdated.Text = string.Empty;
            txtSampleNum.Text = string.Empty;

            lblDistant.Text = string.Empty;
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            DALclient client = new DALclient();
            List<CouponUI> list = new List<CouponUI>();
            if (target.LastUpdated > sample.LastUpdated)
            {
                list = client.findClaimedCouponsBySeries(sample.LastUpdated, target.LastUpdated, GlobalVars.activeUser);
                if (list != null)
                {
                    lblDistant.Text = list.Count.ToString("N0");
                    lblDistant.ForeColor = Color.Green;
                    SortableBindingList<CouponUI> datasource = new SortableBindingList<CouponUI>(list);
                    _parentForm.grvCoupons.DataSource = datasource;
                    _parentForm.grvCoupons.Sort(_parentForm.grvCoupons.Columns["Lastupdated"], ListSortDirection.Descending);
                    _parentForm.formatCouponGrid(_parentForm.grvCoupons);
                }
            }
            else
            {
                list = client.findClaimedCouponsBySeries(target.LastUpdated, sample.LastUpdated, GlobalVars.activeUser);
                if (list != null)
                {
                    lblDistant.Text = list.Count.ToString("N0");
                    lblDistant.ForeColor = Color.Red;
                    SortableBindingList<CouponUI> datasource = new SortableBindingList<CouponUI>(list);
                    _parentForm.grvCoupons.DataSource = datasource;
                    _parentForm.grvCoupons.Sort(_parentForm.grvCoupons.Columns["Lastupdated"], ListSortDirection.Descending);
                    _parentForm.formatCouponGrid(_parentForm.grvCoupons);
                }
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_KEYDOWN = 0x100;
            if (msg.Msg == WM_KEYDOWN)
            {
                if (keyData == Keys.Escape)
                {
                    this.Close();
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void frmCouponLocator_Load(object sender, EventArgs e)
        {
            txtTargetCode.Focus();
        }

    }
}
